<?php
include("config.php");
ini_set('display_errors', 'Off');
include("classes/connect.php");
require_once('classes/class.jira.php');
require_once("classes/class.gmail.php");

$config->username= $jirauser;
$config->password= $jirapass;
$config->port= "";
$config->host= $host;

$queryIssue = new Jira($config);

$sql="SELECT * from trn_emaildetails WHERE deleted='0'";
if(isset($_GET['urlstatus'])) $sql .= "  AND currentStatus='".$request."'";
$query=mysql_query($sql);
while($row = mysql_fetch_array($query)){
	$query1->id=$row['jiraTaskID'];	
	$array=$queryIssue->queryIssue($query1);
	$response = json_decode($array->responseBody);
    
	if(($response->issues[0]->fields->status->name!==$row['currentStatus']) OR ($response->issues[0]->fields->assignee->name!==$row['currentAssignee'])){
        
        $last_changed_by = NULL;
        $last_changed_date = NULL;
        
        $comments_log = $queryIssue->queryComment($response->issues[0]->key);
        $change_log = $queryIssue->queryChangelog($response->issues[0]->key);

        
        if($change_log){
            $res = json_decode($change_log->responseBody);
            if(is_array($res->changelog->histories)){
               $last_pop = count($res->changelog->histories) - 1;
               $last_changed_by = $res->changelog->histories[$last_pop]->author->displayName;
               $last_changed_date = strtotime($res->changelog->histories[$last_pop]->created);
               
            }

        }
        
        $comment_stake = json_decode($comments_log->responseBody);
        
        if(is_array($comment_stake->fields->comment->comments)){
            array_reverse($comment_stake->fields->comment->comments);
            
            // change last update state if comment update time is ahead from change log time
            foreach ($comment_stake->fields->comment->comments as $comment)
            if(strtotime($comment->updated) > $last_changed_date){
                $last_changed_by = $comment->author->displayName;
                $last_changed_date = strtotime($comment->updated);
               
            }
        }
        
		if($response->issues[0]->fields->status->name!==$row['currentStatus']){
            
			$sql1="UPDATE trn_emaildetails SET 
					currentStatus='".$response->issues[0]->fields->status->name."',
					currentAssignee='".$response->issues[0]->fields->assignee->name."',
					currentAssigneeName = '".$response->issues[0]->fields->assignee->displayName."',
                    modifiedBy = '". $last_changed_by ."',
                    modifiedDate = '". $last_changed_date ."' 
					where jiraTaskID='".$row['jiraTaskID']."'
					";
			mysql_query($sql1);
			$historyid=$row['jiraTaskID'];
			$array1=$queryIssue->queryChangelog($historyid);
			$response1 = json_decode($array1->responseBody);
			$reversed_history = array_reverse($response1->changelog->histories);
			
			foreach($reversed_history as $value){
				if($value->items[0]->toString==$response->issues[0]->fields->status->name){
					$modifier=$value->author->name;
					$modifieddate=strtotime($value->created);
					break;
				}			
			}
				switch($response->issues[0]->fields->status->name){
					case $action_one:
						$activitysql = "INSERT INTO trn_activity SET
													mainId = '".$row['id']."',
													status = '".$response->issues[0]->fields->status->name."',
													action = '".$response->issues[0]->fields->status->name."',
													description = '".$action_one_des."',
													modifiedBy = '".$modifier."',
													modifiedDate = '".$modifieddate."'
										";
						mysql_query($activitysql);
						break;
					case $action_two:
						$activitysql = "INSERT INTO trn_activity SET
													mainId = '".$row['id']."',
													status = '".$response->issues[0]->fields->status->name."',
													action = '".$response->issues[0]->fields->status->name."',
													description = '".$action_two_des."',
													modifiedBy = '".$modifier."',
													modifiedDate = '".$modifieddate."'
										";
						mysql_query($activitysql);
						break;
					case $action_three:
						$activitysql = "INSERT INTO trn_activity SET
													mainId = '".$row['id']."',
													status = '".$response->issues[0]->fields->status->name."',
													action = '".$response->issues[0]->fields->status->name."',
													description = '".$action_three_des."',
													modifiedBy = '".$modifier."',
													modifiedDate = '".$modifieddate."'
										";
						mysql_query($activitysql);
						break;
					case $action_four:
						$activitysql = "INSERT INTO trn_activity SET
													mainId = '".$row['id']."',
													status = '".$response->issues[0]->fields->status->name."',
													action = '".$response->issues[0]->fields->status->name."',
													description = '".$action_four_des."',
													modifiedBy = '".$modifier."',
													modifiedDate = '".$modifieddate."'
										";
						mysql_query($activitysql);
						break;
					case $action_five:
						$activitysql = "INSERT INTO trn_activity SET
													mainId = '".$row['id']."',
													status = '".$response->issues[0]->fields->status->name."',
													action = '".$response->issues[0]->fields->status->name."',
													description = '".$action_five_des."',
													modifiedBy = '".$modifier."',
													modifiedDate = '".$modifieddate."'
										";
						mysql_query($activitysql);
						break;
					case $action_six:
						$activitysql = "INSERT INTO trn_activity SET
													mainId = '".$row['id']."',
													status = '".$response->issues[0]->fields->status->name."',
													action = '".$response->issues[0]->fields->status->name."',
													description = '".$action_six_des."',
													modifiedBy = '".$modifier."',
													modifiedDate = '".$modifieddate."'
										";
						mysql_query($activitysql);
						break;
				}
		}
		if($response->issues[0]->fields->assignee->name!==$row['currentAssignee']){
			$sql2="UPDATE trn_emaildetails SET 
					currentAssignee='".$response->issues[0]->fields->assignee->name."',
					currentAssigneeName = '".$response->issues[0]->fields->assignee->displayName."' 
					where jiraTaskID='".$row['jiraTaskID']."'
					";
			mysql_query($sql2);
			
			$historyid=$row['jiraTaskID'];
			$array1=$queryIssue->queryChangelog($historyid);
			$response1 = json_decode($array1->responseBody);
			$reversed_history = array_reverse($response1->changelog->histories);
			
			foreach($reversed_history as $value){
				if($value->items[0]->to==$response->issues[0]->fields->assignee->name){
					$modifier=$value->author->name;
					$modifieddate=strtotime($value->created);
				}			
			}
			$assigneedescription="changed Assignee to <b>".$response->issues[0]->fields->assignee->name."</b> by <b>".$modifier."</b>";
			$activitysql = "INSERT INTO trn_activity SET 
											mainId = '".$row['id']."',
											status = '".$response->issues[0]->fields->status->name."',
											action = '".$response->issues[0]->fields->status->name."',
											description = '".$assigneedescription."',
											modifiedBy = '".$modifier."',
											modifiedDate = '".$modifieddate."'
								";
			mysql_query($activitysql);
		}
	}

}
echo '<meta http-equiv="refresh" content="300;url=cron-jira-sync.php">';

?>